package com.ascentive.finallyfast.deepclean;

//import java.sql.Date;
//import java.text.SimpleDateFormat;
//import java.util.Locale;

import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.CallLog.Calls;

import com.ascentive.finallyfast.FinallyFastApplication;

public class CallMissedHistory extends CallLogHistory {

	int noOfCallsDeleted = 0;
	
	public CallMissedHistory(FinallyFastApplication app, String name,
			boolean value, int img) {
		super(app, name, value, img);

	}

	@Override
	void doClean() {

		int callDeleteStatus = 0;
		// Uri for CALL_LOG
		Uri allCalls = Uri.parse("content://call_log/calls");

		// Define projection
		String[] projection = new String[] { Calls.TYPE, Calls.NUMBER,
				Calls.DATE, Calls.CACHED_NAME, Calls.DURATION };

		// Query the CallLog for details
		// Details from the CallLog to be returned Calls are listed in order of
		// date of occurrence, with recent calls at the top
        
		Cursor c = mApp.getContentResolver().query(allCalls, projection, 
				"date <= (" + Utility.getDateBeforeNDays(mApp.getTargetDateDuration() ) +")", 
				null, Calls.DATE + " DESC");

		// Find past date depend on selected duration
		long pastDate = Utility.getDateBeforeNDays(mApp.getTargetDateDuration() );

		int count = c.getCount();
		// System.out.println("Number of total calls"+ c.getCount());
		if (c.moveToFirst()) {
			do { // For each result returned, the following occurs

//				String phNo =
//				c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
//				String length =
//				c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
				String dateOfCall = c.getString(c
						.getColumnIndex(CallLog.Calls.DATE));
//				String typeOfCall = c.getString(c
//						.getColumnIndex(CallLog.Calls.TYPE));

				long lDateOfCall = (long) Double.parseDouble(dateOfCall);
//
//				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
//		    	String dateString = formatter.format(new Date(lDateOfCall));
		    	
				// System.out.println("Call Date");

				if (lDateOfCall < pastDate) {
//					System.out.println(typeOfCall);
//					if(typeOfCall.equals("3")) // Missed Calls
//					{
//					System.out.println(phNo);
//					System.out.println(length);
//					System.out.println(dateString);
//					}

					// String queryString = "DATE <='" + pastDate + "'";
					String strNumber = "3"; // Missed Calls 
	                String queryString= "TYPE='" + strNumber + "'";

					callDeleteStatus = mApp.getContentResolver().delete(allCalls, queryString,
						 	null);
					noOfCallsDeleted += callDeleteStatus;
				}
			} while (c.moveToNext());
		}

		if (noOfCallsDeleted >= 1) {
			String displayStr = new String();
			displayStr += noOfCallsDeleted;
			displayStr += " Numbers deleted";
			
//			System.out.println("Number of calls deleted =="+ noOfCallsDeleted);
			
//			Toast.makeText(mApp.getApplicationContext(), displayStr,
//					Toast.LENGTH_SHORT).show();
		} else {
//			Toast.makeText(mApp.getApplicationContext(),
//					"No such number in call logs", Toast.LENGTH_SHORT).show();
		}

		return;
	}
	
	@Override
	int numberOfFileCleaned() {
		return noOfCallsDeleted;
	}
}